import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

class FromInputField extends StatelessWidget {
  final Widget leading;
  final String tip;
  final TextEditingController controller;
  final FocusNode focusNode;
  final String helpText;
  final bool hasError;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatterList;
  final Widget trailing;
  final String errorText;
  final ValueChanged<String> onFieldSubmitted;
  final Border border;

  const FromInputField({
    Key key,
    this.helpText,
    this.trailing,
    this.leading,
    this.errorText,
    this.keyboardType,
    this.inputFormatterList,
    this.hasError = false,
    this.tip,
    this.controller,
    this.focusNode,
    this.onFieldSubmitted,
    this.border = const Border(
        bottom: BorderSide(
      color: aceGrayColor,
      width: 0.5,
    )),
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<TextEditingValue>(
      valueListenable: controller,
      builder: (BuildContext context, TextEditingValue value, Widget child) {
        return value.text.isEmpty &&
                !FormEditStatusInheritedWidget.ofEnable(context)
            ? ValueSelectForm(
                title: tip,
                border: border,
              )
            : child;
      },
      child: Stack(
        children: [
          Container(
              constraints: BoxConstraints(minHeight: 62),
              padding: EdgeInsets.symmetric(
                horizontal: getDp(28),
              ),
              decoration: BoxDecoration(border: border),
              child: Row(
                children: [
                  if (leading != null) leading,
                  SizedBox(
                    width: getDp(8),
                  ),
                  Expanded(
                      child: AnimatedBuilder(
                          animation: focusNode, builder: _buildInputField)),
                  if (trailing != null &&
                      FormEditStatusInheritedWidget.ofEnable(context))
                    Padding(
                      padding: EdgeInsets.only(left: 8.0),
                      child: trailing,
                    ),
                ],
              )),
          if (hasError == true)
            Positioned(
              top: 16,
              right: getDp(28),
              child: Text(
                errorText ?? S.current.number_is_taken__please_enter_again,
                style: TextStyle(
                  color: aceRedColor,
                  fontSize: getSp(12),
                  fontWeight: FontWeight.w400,
                  height: 14 / 12,
                ),
              ),
            ),
        ],
      ),
    );
  }

  bool isShowFocusStyle(BuildContext context) =>
      focusNode.hasFocus ||
      !FormEditStatusInheritedWidget.ofEnable(context) ||
      controller.text.isNotEmpty;

  Widget _buildInputField(BuildContext context, Widget child) {
    return TextFormField(
      inputFormatters: inputFormatterList,
      controller: controller,
      focusNode: focusNode,
      scrollPadding: EdgeInsets.zero,
      obscureText: false,
      maxLines: null,
      maxLength: null,
      keyboardType: keyboardType,
      enabled: FormEditStatusInheritedWidget.ofEnable(context),
      style: TextStyle(
          fontSize: getSp(16),
          color: FormEditStatusInheritedWidget.ofEnable(context)
              ? aceBlueColor
              : aceBlackColor,
          fontWeight: FontWeight.w700,
          height: 14 / 16),
      enableSuggestions: true,
      autofillHints: FormEditStatusInheritedWidget.ofEnable(context)
          ? [
              AutofillHints.email,
              AutofillHints.name,
              AutofillHints.countryCode,
              AutofillHints.gender,
              AutofillHints.nickname,
              AutofillHints.jobTitle,
            ]
          : null,
      onFieldSubmitted: onFieldSubmitted,
      decoration: InputDecoration(
          isCollapsed: false,
          border: InputBorder.none,
          focusedBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
          labelText: tip,
          hintText: helpText,
          hintStyle: TextStyle(color: aceBlueColor),
          labelStyle: TextStyle(
              color: hasError
                  ? aceRedColor
                  : (isShowFocusStyle(context)
                      ? aceDarkGrayColor
                      : aceGrayColor),
              fontWeight:
                  isShowFocusStyle(context) ? FontWeight.w400 : FontWeight.w700,
              fontSize: isShowFocusStyle(context) ? getSp(20) : getSp(16),
              height: 16 / 16)),
    );
  }
}
